package com.ccit.goods.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ccit.common.exception.GymException;
import com.ccit.goods.mapper.GoodsMapper;
import com.ccit.goods.mapper.GoodsOrderMapper;
import com.ccit.goods.repository.GoodsOrderRepository;
import com.ccit.goods.service.GoodsOrderService;
import com.ccit.goods.service.GoodsService;
import com.ccit.model.Goods;
import com.ccit.model.GoodsOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Optional;


@Service
@Transactional(rollbackFor = Exception.class)
public class GoodsOrderServiceImpl extends ServiceImpl<GoodsOrderMapper, GoodsOrder> implements GoodsOrderService {
    @Autowired
    private GoodsOrderRepository goodsOrderRepository;

    @Autowired
    private GoodsService goodsService;

    //保存修改订单
    @Override
    public Boolean saveOrder(GoodsOrder goodsOrder) {
        Goods goods = goodsService.getById(goodsOrder.getGoodsId());
        if (goods != null){
            //构建条件
            Example<GoodsOrder> example = Example.of(goodsOrder);
            Optional<GoodsOrder> one = goodsOrderRepository.findOne(example);
            if (one != null&&one.isPresent()){
                GoodsOrder orderExist = one.get();
                        goodsOrder.setTime(orderExist.getTime());
                GoodsOrder save = goodsOrderRepository.save(goodsOrder);
                if (save != null){
                    return true;
                }else return false;
            }else {//订单存在
                goodsOrder.setTime(new Date());
                GoodsOrder save = goodsOrderRepository.save(goodsOrder);
                if (save!=null){
                    return true;
                }else return false;
            }
        }else {
            throw new GymException(202,"参数错误，无此商品");
        }
    }

    //根据手机号查询订单
    @Override
    public List<GoodsOrder> findGoodsOrderByPhone(String phone) {
        List<GoodsOrder> goodsOrderByPhone = goodsOrderRepository.findGoodsOrderByPhoneOrderByTime(phone);
        return goodsOrderByPhone;
    }

    @Override
    public List<GoodsOrder> findGoodsOrderByGoodsId(Long goodsId) {
        List<GoodsOrder> goodsIdLike = goodsOrderRepository.findGoodsOrderByGoodsId(goodsId);
        return goodsIdLike;
    }

    @Override
    public List<GoodsOrder> findGoodsOrderByGoodsIdAndPhone(Long goodsId, String phone) {
        List<GoodsOrder> goodsIdAndPhone = goodsOrderRepository.findGoodsOrderByGoodsIdAndPhone(goodsId, phone);
        return goodsIdAndPhone;
    }

    //条件查询订单
    @Override
    public List<GoodsOrder> findGoodsOrderList(GoodsOrder order) {
        //构建条件
        Example<GoodsOrder> orderExample = Example.of(order);
        List<GoodsOrder> orderList = goodsOrderRepository.findAll(orderExample);
        return orderList;
    }

    //查询所有订单
    @Override
    public List<GoodsOrder> findAllOrder() {
        Sort sort = Sort.by(Sort.Direction.DESC, "time");
        List<GoodsOrder> all = goodsOrderRepository.findAll(sort);
        return all;
    }
}
